package com.paradisecloudtech.vc.system.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.common.library.Utils.LogUtil;
import com.paradisecloudtech.vc.system.R;
import com.common.library.base.BaseActivity;
import com.paradisecloudtech.vc.system.entity.EventBusEvent;
import com.paradisecloudtech.vc.system.ui.fragment.CenterFragment;
import com.paradisecloudtech.vc.system.ui.fragment.SlideFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.dl_main_drawer)
    public DrawerLayout dlMainDrawer;
    @BindView(R.id.fl_main_center)
    public FrameLayout flMainCenter;
    @BindView(R.id.fl_main_slide)
    public FrameLayout flMainSlide;
    @BindView(R.id.tb_main_bar)
    public Toolbar tbMainBar;
    @BindView(R.id.tv_center_toolbar_title)
    public TextView tvCenterToolbarTitle;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    public void intVar() {
        //去掉滑动阴影
        // dlMainDrawer.setScrimColor(Color.TRANSPARENT);
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
    }

    @Override
    public void initData() {
        initToolbar();
        transaction.add(R.id.fl_main_center, CenterFragment.getInstance(), "center");
        transaction.add(R.id.fl_main_slide, SlideFragment.getInstance(), "tag");
        transaction.commit();

        toggle = new ActionBarDrawerToggle(
                this, dlMainDrawer, tbMainBar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        toggle.syncState();
    }

    private void initToolbar() {
        setSupportActionBar(tbMainBar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    @Override
    public void initListener() {
        dlMainDrawer.addDrawerListener(toggle);
        EventBus.getDefault().register(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void SlideItemClick(EventBusEvent.SlideItemClickEvent slideItemClickEvent) {
        //关闭的是toolbar,导航栏图标
        toggle.onDrawerClosed(dlMainDrawer);
        dlMainDrawer.closeDrawer(flMainSlide);
        CenterFragment centerFragment = (CenterFragment) getSupportFragmentManager().findFragmentByTag("center");
        centerFragment.slideItemClick(slideItemClickEvent.getId());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}